package com.gsafety.libraryktx.manager

import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.SPUtils
import com.gsafety.libraryktx.bean.LoginBean
import com.gsafety.libraryktx.config.Constant
import java.util.*

/**获取当前用户信息
 * @author : lj
 * @date : 2020/8/6 9:22
 */
class UserManager private constructor() {
    companion object {
        @Volatile
        private var instance: UserManager? = null

        fun getInstance() =
                instance ?: synchronized(this) {
                    instance ?: UserManager().also { instance = it }
                }
    }

    /**
     * 保存登录
     */
    fun putLogin(login: LoginBean){
        SPUtils.getInstance().put(Constant.Login_KEY,GsonUtils.toJson(login))
    }



    fun getUser(): LoginBean?{
        var user:LoginBean?=null
        var data= SPUtils.getInstance().getString(Constant.Login_KEY)
        if(!data.isNullOrEmpty()){
            user= GsonUtils.fromJson(data, LoginBean::class.java)
        }
        return user
    }


    /**
     * 返回token
     *
     * @return
     */
    fun getToken(): String {
        var token=""
        var loginbean=getUser()
        if(loginbean!=null){
            token=loginbean.sessionId
        }
        return token
    }

    /**
     *菜单权限表
     */
    fun appModelCodes():List<String>?{
        var codes:List<String>?=null
        var loginbean=getUser()
        if(loginbean!=null){
            codes=loginbean.appModelCodes
        }
        return codes

    }


    /**
     *是否是行业单位例如应急局
     */
    fun isLeader():Boolean{
        var leader:Boolean=false
        var loginbean=getUser()
        var result= Arrays.asList(loginbean?.orgType?.split(","))
        var datas=result[0]
        datas?.also {
            if(it.size>1){
                leader=true
            }
        }
        return leader

    }
    fun removeLogin(){
        SPUtils.getInstance().remove(Constant.Login_KEY,true)
    }





}